package com.atguigu.gmall.pms.service.impl;

import com.atguigu.core.bean.Resp;
import com.atguigu.gmall.pms.dao.AttrAttrgroupRelationDao;
import com.atguigu.gmall.pms.dao.AttrDao;
import com.atguigu.gmall.pms.dao.ProductAttrValueDao;
import com.atguigu.gmall.pms.entity.AttrAttrgroupRelationEntity;
import com.atguigu.gmall.pms.entity.AttrEntity;
import com.atguigu.gmall.pms.entity.ProductAttrValueEntity;
import com.atguigu.gmall.pms.vo.AttrGroupVO;
import com.atguigu.gmall.pms.vo.BaseGroupVO;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.core.bean.PageVo;
import com.atguigu.core.bean.Query;
import com.atguigu.core.bean.QueryCondition;

import com.atguigu.gmall.pms.dao.AttrGroupDao;
import com.atguigu.gmall.pms.entity.AttrGroupEntity;
import com.atguigu.gmall.pms.service.AttrGroupService;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    private AttrGroupDao attrGroupDao;


    @Autowired
    private AttrAttrgroupRelationDao attrAttrgroupRelationDao;

    @Autowired
    private AttrDao attrDao;

    @Autowired
    private ProductAttrValueDao productAttrValueDao;


    @Override
    public PageVo queryPage(QueryCondition params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageVo(page);
    }

    /**
     * 查询规格分组
     * @param queryCondition
     * @param cid
     * @return
     */
    @Override
    public PageVo queryByCidPage(QueryCondition queryCondition, Long cid) {

        //通过Query对象获取分页参数来获取page对象，然后通过查询条件，获取结果
        IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(queryCondition)
                , new QueryWrapper<AttrGroupEntity>().eq("catelog_id", cid));

        return new PageVo(page);
    }

    /**
     * 根据分组id查询分组及组下的规格（属性）参数
     * @param gid
     * @return
     */
    @Override
    public AttrGroupVO queryById(Long gid) {
        AttrGroupVO attrGroupVO = new AttrGroupVO();
        //查询当前组信息
        AttrGroupEntity attrGroupEntity = attrGroupDao.selectById(gid);
        //将attrGroupEntity拷贝到attrGroupVO
        BeanUtils.copyProperties(attrGroupEntity,attrGroupVO);
        //根据组信息查询中间表关联的信息
        List<AttrAttrgroupRelationEntity> relationEntityList = attrAttrgroupRelationDao.selectList(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_group_id", gid));
        if (relationEntityList.isEmpty()) {
            return attrGroupVO;
        }
        attrGroupVO.setRelations(relationEntityList);
        //根据关联信息中的属性id，查出所有的属性信息
        List<Long> attrIdList = relationEntityList.stream().map(AttrAttrgroupRelationEntity::getAttrId).collect(Collectors.toList());
        List<AttrEntity> attrEntityList = attrDao.selectBatchIds(attrIdList);
        attrGroupVO.setAttrEntities(attrEntityList);
        return attrGroupVO;
    }

    /**
     * 查询分类下的分组信息及其规格参数信息
     * @param catId
     * @return
     */
    @Override
    public List<AttrGroupVO> queryByCid(Long catId) {
        // 根据cid查询三级分类下的所有属性分组
        List<AttrGroupEntity> attrGroupEntityList =
            attrGroupDao.selectList(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catId));
        // 1.根据分组中id查询中间表
        // 2.根据中间表中的attrIds查询参数
        // 3.数据类型的转化： attrGroup --> gruopVo
        List<AttrGroupVO> attrGroupVOList = attrGroupEntityList.stream()
            .map(attrGroupEntity -> this.queryById(attrGroupEntity.getAttrGroupId())).collect(Collectors.toList());

        return attrGroupVOList;
    }

    @Override
    public List<BaseGroupVO> queryItemGroupVoByCidAndSpuId(Long cid, Long spuId) {

        // 查询当前属性的所有组
        List<AttrGroupEntity> groupEntityList = this.list(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", cid));
        return groupEntityList.stream().map(groupEntity -> {
            BaseGroupVO baseGroupVO = new BaseGroupVO();
            // 设置当前组的名称
            baseGroupVO.setName(groupEntity.getAttrGroupName());

            // 查询当前组关联的所有attrId
            List<AttrAttrgroupRelationEntity> relationEntityList = attrAttrgroupRelationDao.selectList(
                new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_group_id", groupEntity.getAttrGroupId()));
            List<Long> attrIdList =
                relationEntityList.stream().map(AttrAttrgroupRelationEntity::getAttrId).collect(Collectors.toList());
            // 通过spuId和attrId查询对应的规格参数及其值
            List<ProductAttrValueEntity> productAttrValueEntityList = productAttrValueDao
                .selectList(new QueryWrapper<ProductAttrValueEntity>().eq("spu_id", spuId).in("attr_id", attrIdList));
            baseGroupVO.setAttrs(productAttrValueEntityList);
            return baseGroupVO;
        }).collect(Collectors.toList());
    }

}